import TimeShow from '../../utils/TimeShow'
import deleteWei from '../../utils/deleteWei'
Page({
  data: {
    // 是否是编辑状态
    isDelete: false,
    // 全选状态
    isAll: false,
    // 消息数组
    mes: [],
    // 未选中的消息
    xmes: [],
    // 要删除的通知id
    did: [],
  },

  onLoad: function (options) {
    this.handAll()
    this.handAll()
    this.getAllXT()
  },
  // 改变编辑状态
  handChang() {
    this.setData({
      isDelete: !this.data.isDelete,
    })
  },
  // 选中状态
  handCheck(e) {
    const allId = this.getAllId()
    const { value } = e.detail
    const { mes } = this.data
    const wm = []
    if (value.length === allId.length) {
      this.setData({
        isAll: true,
      })
    } else {
      this.setData({
        isAll: false,
      })
    }
    mes.forEach(item1 => {
      if(!(value.includes(item1.id.toString()))) {
        wm.push(item1)
      }
    })
    console.log(wm)
    this.setData({
      did: value,
      xmes: wm
    })
  },
  // 全选按钮
  handAll() {
    const { mes } = this.data
    const mm = []
    mes.forEach((item) => {
      item.checked = !this.data.isAll
      mm.push(item)
    })
    this.setData(
      {
        isAll: !this.data.isAll,
        mes: mm,
      },
      () => {
        if (this.data.isAll) {
          this.setData({
            xmes: [],
            did: this.getAllId(),
          })
        } else {
          this.setData({
            xmes: this.data.mes,
            did: [],
          })
        }
      }
    )
  },
  // 删除选中通知
  deleteMes() {
    this.setData({
      mes: this.data.xmes,
      isDelete: false
    })
  },
  // 获取所有通知ID
  getAllId() {
    const { mes } = this.data
    const allid = []
    mes.forEach((item) => {
      allid.push(item.id)
    })
    return allid
  },
  
  // 获取所有系统通知
  async getAllXT() {
    let xitong = wx.getStorageSync('xitong')
    if(xitong.length < 0 || !xitong) return
    let hids = []
    xitong.forEach((item, index) => {
      if(item.isweidu) {
        getApp().globalData.wdxx -= 1
        if(!hids.includes(item.userId) && item['isku']) hids.push(item.userId)
      }
      item['id'] = index
      const newTime = item['stime'].split('/')
      item['time'] = `${newTime[0]}-${newTime[1]}-${newTime[2]}`
      item['time'] = TimeShow(item['time'])
      item.isweidu = false
    })
    if(hids.length > 0) deleteWei(hids)
    xitong.reverse()
    wx.setStorageSync('xitong', xitong)
    this.setData({
      mes: xitong
    })
  }
})
